﻿using System;
using System.Web;
using Jumpton.SFA.Server.BusinessRule;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;

public class Image : IHttpHandler {



    public void ProcessRequest(HttpContext context)
    {
        context.Response.ClearContent();
        context.Response.ContentType = "image/Gif";
        String ImagePath = System.Configuration.ConfigurationManager.AppSettings["ImagePath"];
        try
        {

            var photo = new PhotoManager().Load(context.Request.QueryString.ToString());
            if (photo != null)
            {
                String FileName = ImagePath + "\\" + photo.PHOTOPATH;
                if (File.Exists(FileName))
                {
                    FileStream stream = null;
                    try
                    {
                        stream = File.OpenRead(FileName);
                        byte[] data = new byte[stream.Length];
                        stream.Read(data, 0, data.Length);
                        context.Response.BinaryWrite(data);
                        context.Response.Flush();
                    }
                    finally
                    {
                        if (stream != null)
                        {
                            stream.Close();
                            stream.Dispose();
                            stream = null;
                        }
                    }

                }
            }

        }
        catch { }

    }
 
    /// <summary> 
    /// 放大缩小图片尺寸 
    /// </summary> 
    /// <param name="picPath"></param> 
    /// <param name="reSizePicPath"></param> 
    /// <param name="iSize"></param> 
    /// <param name="format"></param> 
    public Stream PicSized(String FileName, int width, int height, ImageFormat format)
    {
        MemoryStream ms = new MemoryStream();
        Bitmap originBmp = new Bitmap(FileName);
        int w = width * width/originBmp.Width;
        int h = height * height/originBmp.Height;
        Bitmap resizedBmp = new Bitmap(w, h);
        Graphics g = Graphics.FromImage(resizedBmp);
        //设置高质量插值法   
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        //设置高质量,低速度呈现平滑程度   
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        //消除锯齿 
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        g.DrawImage(originBmp, new Rectangle(0, 0, w, h), new Rectangle(0, 0, originBmp.Width, originBmp.Height), GraphicsUnit.Pixel);
        resizedBmp.Save(ms, format);
        g.Dispose();
        resizedBmp.Dispose();
        originBmp.Dispose();

        return ms;
    }
   
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}